/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pazi_laba1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;


/**
 *
 * @author Mike
 */
public class Admin_handler {
public DefaultListModel L_M(String filename) {
        
            DefaultListModel list = new DefaultListModel();           
            try {
              BufferedReader  bf = new BufferedReader(new FileReader(filename));
            
            String line = "";
            String atr = "";
            bf. readLine();
            list.addElement("ADMIN--(можно все)");
            while ((line = bf.readLine()) != null) {
                atr += line+"--Пароль: "+bf.readLine()+ "--(" + bf.readLine()+")--";
                list.addElement(atr);
              
                atr="";
            }
            bf.close();
        } catch (IOException ex) {
            Logger.getLogger(Admin_handler.class.getName()).log(Level.SEVERE, null, ex);
        } return list;
}
public void Add_user(String filename,String username) {
        try {
             BufferedWriter  bf = new BufferedWriter(new FileWriter(filename, true));
             bf.newLine();
             bf.write(username);
             bf.newLine();
             bf.newLine();
             bf.write("5111");
             bf.close();
        } catch (IOException ex) {
            Logger.getLogger(Admin_handler.class.getName()).log(Level.SEVERE, null, ex);
        }
}
public String Read_rules(String filename,String username){
String rules="";
  try {
            BufferedReader  bf = new BufferedReader(new FileReader(filename));
            if(username.equals("ADMIN")){
              rules="0111"  ;
            }
            else{
            String line = "";
            while ((line = bf.readLine()) != null) {
                if(line.equals(username)){
                     bf.readLine();
                     rules= bf.readLine();
                }
            }}
            bf.close();
        } catch (IOException ex) {
            Logger.getLogger(Admin_handler.class.getName()).log(Level.SEVERE, null, ex);
        }
return rules;
}
public void Change_rules(String filename,String username,String rules){
     List<String> buffer=new ArrayList<String>();
    try {
            BufferedReader  bf = new BufferedReader(new FileReader(filename));
            String line="";
            while ((line = bf.readLine()) != null) {
               buffer.add(line);
            }
            bf.close();
        } catch (IOException ex) {
            Logger.getLogger(Admin_handler.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(int i=0;i<buffer.size();i++){
            if(buffer.get(i).equals(username)){
             buffer.add(i+2, rules);
            }
        }
      try {
             BufferedWriter  bf = new BufferedWriter(new FileWriter(filename));
             for(int i=0;i<buffer.size();i++){
             bf.write(buffer.get(i));
             bf.newLine();
             }
             bf.close();
        } catch (IOException ex) {
            Logger.getLogger(Admin_handler.class.getName()).log(Level.SEVERE, null, ex);
        }
}
}
